'use strict';

/* Controllers */
// signin controller
app.controller('registerFormController',
    function(
        $scope,
        $interval,
        $localStorage,
        $state,
        rest,
        user,
        $location,
        $ocLazyLoad,
        uiLoad,
        tipService) {
        $scope.user = {
            Phone: user.get('phone'),
            PasswordType: 2
        };
        $scope.authError = null;
        $scope.signinShow = false;
        $scope.sms = {
            totalSecond: 60,
            sending: false,
            btnText: "获取验证码",
            promise: ""
        };

        // 记录的登录状态
        if (user.get('landlordId') && user.get('openId')) {
            $state.go("nav.widget");
        }
        $scope.$watch('Password', function(newValue, oldValue) {
            $scope.user.Password = newValue;
        })
        $scope.$watch('ConfirmPassword', function(newValue, oldValue) {
            $scope.user.ConfirmPassword = newValue;
        })
        $scope.forgetPsw = function() {
            $scope.forgetPsw = !$scope.forgetPsw;
        }
        $scope.signToggle = function() {
            $scope.signinShow = !$scope.signinShow;
        }

        var countingStart = function() {
            // first of all, cancel all counting
            $interval.cancel($scope.sms.promise);

            var counting = $scope.sms.totalSecond;
            $scope.sms.sending = true;

            $scope.sms.btnText = "重新获取(" + (counting--) + ")";

            $scope.sms.promise = $interval(function() {
                $scope.sms.btnText = "重新获取(" + (counting--) + ")";
                if (counting < 0) {
                    $scope.sms.btnText = "获取验证码";
                    $scope.sms.sending = false;
                    counting = $scope.sms.totalSecond;
                    $interval.cancel($scope.sms.promise);
                }
            }, 1000);
        };

        $scope.smscode = function() {
            // if (!$scope.user.Phone) {
            //     return false;
            // }

            delete $localStorage.user;

            if ($scope.user.Phone.length != 11 || $scope.user.Phone.charAt(0) != 1) {
                tipService.show("body", {
                    message: "手机号格式输入错误",
                    notice: "danger"
                }, 3);
                return;
            }
            countingStart();

            rest.post({
                methodId: 4003,
                data: {
                    Phone: $scope.user.Phone
                },
                success: function(result) {
                    console.log(result);
                }
            })

            return false;
        };
        $scope.login = function() {

            if (!$scope.user.Phone) {
                return false;
            }

            if (!$scope.user.smsCode && !$scope.user.password) {
                return false;
            }

            var _scope = $scope,
                _state = $state;
            rest.post({
                methodId: 4002,
                data: {
                    phone: $scope.user.Phone,
                    code: $scope.user.smsCode,
                    password: $scope.user.password
                },
                success: function(result) {
                    if (result) {
                        if ("0" === result.code) {
                            _scope.authError = "登录成功";
                            $localStorage.user = result.res.data;
                            _scope.$digest();
                            // location.href = "#/nav/widget";
                            _state.go("nav.widget");
                        } else {
                            _scope.authError = result.res.msg;
                            _scope.$digest();
                        }
                    }
                }
            })

            return false;
        };
        $scope.reset = function() {
            if ($scope.user.smsCode != $scope.user.smsCode) {
                tipService.show("body", {
                    message: "验证码输入错误",
                    notice: "danger"
                }, 3);
                return;
            }
            rest.post({
                methodId: 5018,
                data: $scope.user,
                success: function(res) {
                    if (res.code == 0) {
                        tipService.show("body", {
                            message: "密码重置成功"
                        }, 3);
                        $scope.forgetPsw = !$scope.forgetPsw;
                    } else {
                        tipService.show("body", {
                            message: "密码重置失败!",
                            notice: "danger"
                        }, 3);
                    }
                },
                error: function() {

                }
            })
        }
        $scope.regi = function() {
            if (!$scope.user.Phone) {
                return false;
            }

            if (!$scope.user.smsCode) {
                return false;
            }

            // if (!$scope.user.realName) {
            //     return false;
            // }

            if (!$scope.user.Password) {
                return false;
            }
            if ($scope.user.Password.length != 6) {
                tipService.show("body", {
                    message: "需要输入6位登录密码",
                    notice: "danger"
                }, 3);
                return;
            }
            if ($scope.user.Password != $scope.user.ConfirmPassword) {
                tipService.show("body", {
                    message: "两次密码输入不一致",
                    notice: "danger"
                }, 3);
                return;
            }
            var _scope = $scope,
                _state = $state;
            rest.post({
                methodId: 4001,
                data: {
                    Phone: $scope.user.Phone,
                    Code: $scope.user.smsCode,
                    RealName: "zyz",
                    Email: "123",
                    Password: $scope.user.Password,
                    ConfirmPassword: $scope.user.ConfirmPassword
                },
                success: function(result) {
                    if (result) {
                        if ("0" === result.code) {
                            _scope.authError = "注册成功";
                            _scope.$digest();
                            $localStorage.user = result.res.data;
                            // location.href = "#/nav/widget";
                            _state.go("nav.widget");
                        } else {
                            _scope.authError = result.res.msg;
                            _scope.$digest();
                        }
                    }
                },

            })

            return false;
        };


    });